﻿Public Class VSTYPE
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_MASVSTYPE As New PROVIDERSCLASS

    Private Sub VSTYPE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridvstype()

        DesignForm(Me)
        DesignGrid(MASVSTYPEGrid)

        PROVIDER_MASVSTYPE.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridvstype()
        Dim objSelectDataVstype As New VSTYPECLASS
        Dim OperatData As DataTable
        OperatData = connect.GetTable(objSelectDataVstype.selectMasvstype_)
        MASVSTYPEGrid.PrimaryGrid.DataSource = OperatData
    End Sub

    Private Sub MASVSTYPEGrid_CellClick_1(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASVSTYPEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสการวัดชีพ").Value() IsNot DBNull.Value Then
            VSIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสการวัดชีพ").Value()
        Else
            VSIDTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("การวัดชีพ").Value() IsNot DBNull.Value Then
            VSDESCTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("การวัดชีพ").Value()
        Else
            VSDESCTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ค่าต่ำสุด").Value() IsNot DBNull.Value Then
            LLTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ค่าต่ำสุด").Value()
        Else
            LLTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ค่าสูงสุด").Value() IsNot DBNull.Value Then
            HLTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ค่าสูงสุด").Value()
        Else
            HLTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วย").Value() IsNot DBNull.Value Then
            UNITTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หน่วย").Value()
        Else
            UNITTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำแหน่งที่โชว์").Value() IsNot DBNull.Value Then
            LSTORDERTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำแหน่งที่โชว์").Value()
        Else
            LSTORDERTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("MIN").Value() IsNot DBNull.Value Then
            MINTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("MIN").Value()
        Else
            MINTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("MAX").Value() IsNot DBNull.Value Then
            MAXTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("MAX").Value()
        Else
            MAXTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("F_OPD").Value() = True Then
            FOPDChk.Checked = True
        Else
            FOPDChk.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("F_IPD").Value() = True Then
            FIPDChk.Checked = True
        Else
            FIPDChk.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("F_ER").Value() = True Then
            FERChk.Checked = True
        Else
            FERChk.Checked = False
        End If
      

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            masvstypeStatusCheckbox.Checked = True
        Else
            masvstypeStatusCheckbox.Checked = False
        End If

        connect = CONDB.NewConnection()
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสการวัดชีพ").Value() > 8 Then
            If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
                Try
                    Dim Sql As String
                    connect.BeginTrans()
                    Sql = "UPDATE MASVSTYPE SET "
                    If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                        Sql += "`STATUS` = 1 "
                    Else
                        Sql += "`STATUS` = 0 "
                    End If
                    Sql += "WHERE `VSID` = '" & VSIDTxt.Text & "' ;"
                    connect.ExecuteNonQuery(Sql)
                    connect.CommitTrans()
                Catch ex As Exception
                    connect.RollbackTrans()
                    MsgBox(ex.ToString)
                End Try
            End If
            connect.Dispose()
        End If
        
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASRELIGION SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [RELIGION] = '" & RELIGIONIDTxt.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addMasvstype.Enabled = False
    End Sub

    Private Sub LLTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub
    Private Sub HLTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub VSDESCTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If VSDESCTxt.Text.Length > 14 Then
            e.KeyChar = ""
        End If
    End Sub

    Private Sub LSTORDERTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub MINTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub MAXTxt_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub addMasvstype_Click_1(sender As Object, e As EventArgs) Handles addMasvstype.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASVSTYPE.PADD_ = True Then
                If VSDESCTxt.Text.ToString.Length > 0 And LLTxt.Text.ToString.Length > 0 And HLTxt.Text.ToString.Length > 0 And LSTORDERTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New VSTYPECLASS
                    objInsertData.VSDESC_ = VSDESCTxt.Text
                    objInsertData.LL_ = LLTxt.Text
                    objInsertData.HL_ = HLTxt.Text
                    objInsertData.UNIT_ = UNITTxt.Text
                    objInsertData.LSTORDER_ = LSTORDERTxt.Text
                    objInsertData.MIN_ = MINTxt.Text
                    objInsertData.MAX_ = MAXTxt.Text
                    objInsertData.vstypeStatus_ = masvstypeStatusCheckbox.Checked
                    objInsertData.F_OPD_ = FOPDChk.Checked
                    objInsertData.F_IPD_ = FIPDChk.Checked
                    objInsertData.F_ER_ = FERChk.Checked
                    objInsertData.insertMasvstypeData()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If

                SelectDatagridvstype()

                VSDESCTxt.Text = ""
                LLTxt.Text = ""
                HLTxt.Text = ""
                UNITTxt.Text = ""
                LSTORDERTxt.Text = ""
                MINTxt.Text = ""
                MAXTxt.Text = ""
                masvstypeStatusCheckbox.Checked = False
            Else
                MsgBox(PROVIDER_MASVSTYPE.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasvstype_Click_1(sender As Object, e As EventArgs) Handles editMasvstype.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASVSTYPE.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New VSTYPECLASS
                    objEditData.VSID_ = VSIDTxt.Text
                    objEditData.VSDESC_ = VSDESCTxt.Text
                    objEditData.LL_ = LLTxt.Text
                    objEditData.HL_ = HLTxt.Text
                    objEditData.UNIT_ = UNITTxt.Text
                    objEditData.LSTORDER_ = LSTORDERTxt.Text
                    objEditData.MIN_ = MINTxt.Text
                    objEditData.MAX_ = MAXTxt.Text
                    objEditData.vstypeStatus_ = masvstypeStatusCheckbox.Checked
                    objEditData.F_OPD_ = FOPDChk.Checked
                    objEditData.F_IPD_ = FIPDChk.Checked
                    objEditData.F_ER_ = FERChk.Checked
                    objEditData.editMasvstypeData()

                    SelectDatagridvstype()

                    VSDESCTxt.Text = ""
                    LLTxt.Text = ""
                    HLTxt.Text = ""
                    UNITTxt.Text = ""
                    LSTORDERTxt.Text = ""
                    MINTxt.Text = ""
                    MAXTxt.Text = ""
                    masvstypeStatusCheckbox.Checked = False
                End If
                addMasvstype.Enabled = True
            Else
                MsgBox(PROVIDER_MASVSTYPE.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masvstype.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masvstype.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub btnClearForm_Click(sender As Object, e As EventArgs) Handles btnClearForm.Click
        addMasvstype.Enabled = True

        SelectDatagridvstype()

        VSIDTxt.Text = ""
        VSDESCTxt.Text = ""
        LLTxt.Text = ""
        HLTxt.Text = ""
        UNITTxt.Text = ""
        LSTORDERTxt.Text = ""
        MINTxt.Text = ""
        MAXTxt.Text = ""
        masvstypeStatusCheckbox.Checked = False
        FOPDChk.Checked = False
        FIPDChk.Checked = False
        FERChk.Checked = False

    End Sub
   
   
End Class